package com.xydtech.web.controller.system;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.system.domain.SysWechatApplet;
import com.xydtech.system.service.ISysWechatAppletService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 微信小程序配置Controller
 *
 * @author JHON
 * @date 2021-10-12
 */
@Controller
@RequestMapping("/system/applet")
public class SysWechatAppletController extends BaseController {
    private String prefix = "system/applet";

    @Autowired
    private ISysWechatAppletService sysWechatAppletService;

    @RequiresPermissions("system:applet:view")
    @GetMapping()
    public String applet() {
        return prefix + "/applet";
    }

    /**
     * 查询微信小程序配置列表
     */
    @RequiresPermissions("system:applet:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysWechatApplet sysWechatApplet) {
        startPage();
        List<SysWechatApplet> list = sysWechatAppletService.selectSysWechatAppletList(sysWechatApplet);
        return getDataTable(list);
    }

    /**
     * 导出微信小程序配置列表
     */
    @RequiresPermissions("system:applet:export")
    @Log(title = "微信小程序配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysWechatApplet sysWechatApplet) {
        List<SysWechatApplet> list = sysWechatAppletService.selectSysWechatAppletList(sysWechatApplet);
        ExcelUtil<SysWechatApplet> util = new ExcelUtil<SysWechatApplet>(SysWechatApplet.class);
        return util.exportExcel(list, "applet");
    }

    /**
     * 新增微信小程序配置
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存微信小程序配置
     */
    @RequiresPermissions("system:applet:add")
    @Log(title = "微信小程序配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysWechatApplet sysWechatApplet) {
        SysWechatApplet applet = new SysWechatApplet();
        applet.setAppletType(sysWechatApplet.getAppletType());
        applet.setDeptId(sysWechatApplet.getDeptId());
        List<SysWechatApplet> applets = sysWechatAppletService.selectSysWechatAppletList(applet);
        if (applets.size() > 0) {
            return error("该机构下已有相同小程序类型的数据，不可以重复配置！");
        }
        SysWechatApplet wechatApplet = new SysWechatApplet();
        wechatApplet.setAppid(sysWechatApplet.getAppid());
        wechatApplet.setDeptId(sysWechatApplet.getDeptId());
        List<SysWechatApplet> wechatApplets = sysWechatAppletService.selectSysWechatAppletList(wechatApplet);
        if (wechatApplets.size() > 0) {
            return error("该机构下已有相同小程序ID的数据，不可以重复配置！");
        }
        return toAjax(sysWechatAppletService.insertSysWechatApplet(sysWechatApplet));
    }

    /**
     * 修改微信小程序配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SysWechatApplet sysWechatApplet = sysWechatAppletService.selectSysWechatAppletById(id);
        mmap.put("sysWechatApplet", sysWechatApplet);
        return prefix + "/edit";
    }

    /**
     * 修改保存微信小程序配置
     */
    @RequiresPermissions("system:applet:edit")
    @Log(title = "微信小程序配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysWechatApplet sysWechatApplet) {
        return toAjax(sysWechatAppletService.updateSysWechatApplet(sysWechatApplet));
    }

    /**
     * 删除微信小程序配置
     */
    @RequiresPermissions("system:applet:remove")
    @Log(title = "微信小程序配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysWechatAppletService.deleteSysWechatAppletByIds(ids));
    }
}
